﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.Client;
using GoogleCalendarDemo;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Calendar.v3;
using System.Text.RegularExpressions;
using RestSharp;

namespace Tools
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Main());
        }

        private static void GetRiLi()
        {
            var rili = "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgBGAAAD63Q7MReF3U_07sh48tkOlAcA2ME1sP4OHkG8uWM1VkagLgAAAgEGAAAA2ME1sP4OHkG8uWM1VkagLgAAAvzuAAAA";
            var ribao = "AQMkADAwATY3ZmYAZS1hOGQ5LWI1NjEtMDACLTAwCgBGAAAD63Q7MReF3U_07sh48tkOlAcA2ME1sP4OHkG8uWM1VkagLgAAAgEGAAAA2ME1sP4OHkG8uWM1VkagLgADaKnBpQAAAA==";

            var time = Convert.ToDateTime("2021-12-07");
            for (int i = 0; i < 600; i++)
            {
                var list = new MicrosoftCalendar().GetCalendarByID(time, rili);
                var list1 = new MicrosoftCalendar().GetCalendarByID(time, ribao);
                time = time.AddDays(1);

                foreach (var item in list)
                {
                    WriteGoogle(item);
                }
                foreach (var item in list1)
                {
                    WriteGoogle(item);
                }
                Console.WriteLine(time.ToString("yyyy-MM-dd"));
                if (list.Count > 0 || list1.Count > 0)
                {
                    System.Threading.Thread.Sleep(500);
                }
            }

            Console.WriteLine("OK");
            Console.ReadKey();
        }

        public static Tools.Helper.Google google = new Tools.Helper.Google();

        private static void WriteGoogle(Calendar.CalenderModel obj)
        {
            Event newEvent = new Event()
            {
                Summary = obj.Title,
                Location = "沈阳市, 中国辽宁省沈阳市",
                Description = obj.Body,

                Start = new EventDateTime()
                {
                    DateTime = obj.Start,
                },
                End = new EventDateTime()
                {
                    DateTime = obj.End,
                }
            };

            String calendarId = "primary";
            EventsResource.InsertRequest request = google.Service.Events.Insert(newEvent, calendarId);
            Event createdEvent = request.Execute();
        }

        private static void tqyb()
        {
            //2018/11/5
            for (int y = 2019; y < 2023; y++)
            {
                for (int k = 1; k <= 12; k++)
                {
                    var yue = k.ToString();
                    if (k < 10)
                    {
                        yue = "0" + k;
                    }

                    var str = RestSharpGet($"https://lishi.tianqi.com/shenyang/{y}{yue}.html");
                    str = Regex.Split(str, "<div class=\"tian_three\">")[1];
                    str = Regex.Split(str, "<div class=\"lishidesc2\">查看更多")[0];
                    str = Regex.Split(str, "<ul class=\"thrui\">")[1];
                    str = Regex.Replace(str, "<div(.*?)>", "<div>");
                    str = Regex.Replace(str, "<div></div>", "");

                    string pattern = @"<div>(.*?)</div>";
                    RegexOptions options = RegexOptions.Multiline;

                    var list = new List<string>();
                    foreach (Match m in Regex.Matches(str, pattern, options))
                    {
                        list.Add(m.Groups[1].Value.Trim());
                    }

                    var ret = "";
                    var ls = "";
                    for (int i = 1; i <= list.Count; i++)
                    {
                        ret += ls + list[i - 1].ToString();
                        ls = ",";
                        if (i % 5 == 0 && i > 0)
                        {
                            var items = ret.Split(',');
                            //Console.WriteLine($"{items[0]}|{items[1]}|{items[2]}|{items[3]}|{items[4]}");
                            //添加事件
                            WriteGoogle(new Calendar.CalenderModel() { Title = $"{items[3]},{items[1]}到{items[2]},{items[4]}", Start = Convert.ToDateTime($"{items[0].Split(' ')[0]} 6:00"), End = Convert.ToDateTime($"{items[0].Split(' ')[0]} 7:00") });

                            Console.WriteLine(ret);
                            ret = "";
                            ls = "";
                        }
                    }

                    Console.WriteLine(ret);
                    Console.WriteLine("\r\n");
                    System.Threading.Thread.Sleep(500);
                } 
            }
        }
        public static string RestSharpGet(string url, int timeOut = 10)
        {
            try
            {
                var client = new RestClient(url);
                var request = new RestRequest(Method.GET);
                request.Timeout = timeOut * 1000;


                IRestResponse response = client.Execute(request);
                return response.Content;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
